package com.xxxx.rpc.service;

import com.xxxx.api.service.GoodsCategoryService;
import com.xxxx.api.pojo.GoodsCategory;
import com.xxxx.api.pojo.GoodsCategoryExample;
import com.xxxx.api.pojo.GoodsCategoryVo;
import com.xxxx.rpc.mapper.GoodsCategoryMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *   商品分类服务实现类
 */
@Service(version = "1.0")
public class GoodsCategoryImpl implements GoodsCategoryService {
    //第一步 拿到对应的Mapper层
    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;
   /**
    * 商品分类-新增分类-查询所有顶级分类
    */
    @Override
    public List<GoodsCategory> selectCategoryTopList() {
        //创建查询对象
        GoodsCategoryExample example = new GoodsCategoryExample();
        //设置查询条件，parent_id=0
        example.createCriteria().andParentIdEqualTo((short) 0);
        return goodsCategoryMapper.selectByExample(example);
    }

    /**
     * 商品 新增分类级联查询
     * @param parentId
     * @return
     */
    @Override
    public List<GoodsCategory> selectCategoryByParentId(Short parentId) {
        //创建查询对象
        GoodsCategoryExample example = new GoodsCategoryExample();
        //设置查询条件，parent_id=0
        example.createCriteria().andParentIdEqualTo(parentId);
        return goodsCategoryMapper.selectByExample(example);
    }

    /**
     * 商品分类-新增分类-保存分类
     * @param goodsCategory
     * @return
     */
    @Override
    public int categorySave(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.insertSelective(goodsCategory);
    }

    /**
     * 商品分类 列表
     * @return
     */
    @Override
    public List<GoodsCategoryVo> selectCtegoryListTable() {
        //1.查顶级分类  创建查询对象
        GoodsCategoryExample example = new GoodsCategoryExample();
        //设置查询条件 where parent_id = 0 and level = 1
        example.createCriteria().andParentIdEqualTo((short)0).andLevelEqualTo((byte)1);
        //查询顶级分类
        List<GoodsCategory> gcList = goodsCategoryMapper.selectByExample(example);
        List<GoodsCategoryVo> gcvList = new ArrayList<>();
        for(GoodsCategory gc001 : gcList){
            GoodsCategoryVo gcv001 = new GoodsCategoryVo();
            //2. 把GoodsCategorygory对象转成GoodsCategoryVo对象
            BeanUtils.copyProperties(gc001,gcv001);
            //清空查询对象 准备做二级查询
            example.clear();
            //新的查询条件
            example.createCriteria().andParentIdEqualTo(gc001.getId()).andLevelEqualTo((byte) 2);
            //查询二级分类
            List<GoodsCategory> gcList02 = goodsCategoryMapper.selectByExample(example);
            List<GoodsCategoryVo> gcvList02 = new ArrayList<>();
            for(GoodsCategory gc02 : gcList02){
                GoodsCategoryVo gcv02 = new GoodsCategoryVo();
                BeanUtils.copyProperties(gc02,gcv02);
                //清空查询对象
                example.clear();
                //设置查询条件
                example.createCriteria().andParentIdEqualTo(gc02.getId()).andLevelEqualTo((byte) 3);

                //查询三级分类
                List<GoodsCategory> gcList03 = goodsCategoryMapper.selectByExample(example);
                List<GoodsCategoryVo> gcvList03 = new ArrayList<>();
                for (GoodsCategory gc03 : gcList03) {
                    GoodsCategoryVo gcv03 = new GoodsCategoryVo();
                    BeanUtils.copyProperties(gc03,gcv03);
                    gcvList03.add(gcv03);
                }

                //把三级分类list放入二级分类的对象里
                gcv02.setChildren(gcvList03);
                gcvList02.add(gcv02);
            }

            //把二级分类list放入一级分类的对象里
            gcv001.setChildren(gcvList02);
            gcvList.add(gcv001);

        }
        return gcvList;
    }

    @Override
    public List<GoodsCategory> selectCategoryList() {
        return goodsCategoryMapper.selectByExample(new GoodsCategoryExample());
    }
}
